<?php
namespace Shop\Controller;
use Think\Controller;
use Think\Verify; //验证码
use Think\Page;  //分页
use Think\Upload;            //使用上传功能

header("Content-type:text/html;charset=utf-8");
class IndexController extends Controller
{
//用户注册
    public function _before_register()
    {
        if (IS_POST) {
            $u = D('username');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $c['username'] = $data['username'];
            if ($data['username'] == '') {
                $this->error('用户名不能为空', U("register", 5));
            }
            if ($data['password'] == '') {
                $this->error('密码不能为空', U("register", 5));
            }
            if ($data['password'] !== I('post.password2')) {
                $this->error('两次密码不相同', U("register", 5));
            }
            if ($data['email'] == '') {
                $this->error('邮箱不能为空', U("email", 5));
            }
            if ($u->where($c)->count()) {
                $this->error("用户名已存在");
            }
            $u->add($data);
//            var_dump($u->add($data));exit;
            $vercode = $_POST['vercode'];
//            var_dump($vercode);
            $ver = new Verify();
            $u = $ver->check($vercode);
            if ($u) {
                $this->success("注册成功", U("reg", 5));
            } else {
                $this->error('验证码错误');
            }
            exit;//不中断的话会执行后面的register方法里的display代码
        }
    }
    public function register()
    {
        $this->display();
    }
//验证码
    public function vercode(){
        $ver = new Verify();
        $ver->entry();
    }
//用户登录
    public function _before_reg(){
        if (IS_POST){
            $u = M('username');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $userArr = $u->where($data)->find();
//            var_dump($userArr);
//            exit;
            if ($userArr){
                session('username_id',$userArr['id']);//写凭证
                $this->success('登录成功',U('shouye'));
            } else {
                $this->error('用户名或密码不正确');
            }
            exit;
        }
    }
    public function reg(){
        $this->display();
    }
//管理员登录
    public function _before_Admin(){
        if (IS_POST){
            $u = M('guangliyuan');
            $data = $u->create();
//            var_dump($data);
//            exit;
            $userArr = $u->where($data)->find();
//            var_dump($userArr);
//            exit;
            if ($userArr){
                session('Adusername_id',$userArr['id']);//不能跟用户的session信息相等
                $this->success('登录成功',U('Adxiaoxi')); //直接进入管理后台
            } else {
                $this->error('用户名或密码不正确');
            }
            exit;
        }
    }
    public function Admin(){
        $this->display();
    }

//用户退出登录
    public function Out(){
        session('username_id',null);
        $this->success('退出成功',U('shouye'));
    }

//管理员退出登录
    public function AdOut(){
        session('Adusername_id',null);
        $this->success('退出成功',U('/Shop/index/shouye'));
    }


//商品上传
    public function save(){
        $article = M('shangping');           //1.参数，表名
        $data = $article->create();

        $upload = new Upload();
        $upload->mimes = array('image/png', 'image/gif', 'image/jpeg', 'image/jpg');
        $upload->maxSize = 1024 * 1024;
        $upload->exts = array('jpg', 'png', 'gif', 'png');
        $upload->savePath = 'image2/';        //保存到指定文件夹中

        $r = $upload->uploadOne($_FILES['phone']);
        $file = $r['savepath'] . $r['savename'];

        $u = M('shangping');
        $data['phone'] = $file;              //字段 photo   =  文件字符串            赋值

        if (!$file == "") {
            $k = 1;             //把文件字符串传进数据库中
            if ($k) {
                $this->success('上传成功', U('/Shop/index/Adxiaoxi'), 1);
            } else {
                $this->error('上传失败');
            }
        } else {
            $this->error('图片不能为空');
        }

        if (!$data['sp_ming'] == "") {
            if (!$data['sp_id'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
//                        $this->success("", U('/Shop/Index/Adxinxi'));
                    }//else {
//                     $this->error("发布失败");
//                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }

//首页
    public function shouye(){
        $article = M("shangping");                //数据表名
        $p=I("get.page");

        $totolCount=$article->count();
        $pager=new Page($totolCount,8);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');

        //普通用户
        $u = M('username');
//        var_dump(session('username_id'));
        if(!session('username_id')==null) {
            $user = $u->find(session('username_id'));
//            var_dump($user);
        }
        $this->assign('user',$user);

        $data=$article->page($p,8)->select();
        $this->assign('lists',$data);

        $this->assign('pager',$pager);

        $this->display();
    }

//用户个人资料
    public function per(){
        $u = M('username');
        $data = $u->create();
        $data['username_id']=session('username_id');
//        var_dump($data);
        $data = $u->find();
        $this->assign('user',$data);
        $this->display();
    }
//商品详情
    public function xiangqing(){
        $article=M("shangping");
        $id=$_GET['id'];


        $u = M('username');
        $ul = $u->create();
        $ul['username_id']=session('username_id');
//      var_dump($ul);
        if ($ul['username_id']){
            $ul = $u->find($ul['username_id']);
        } else {
            $ul = null;
        }
//        var_dump($u->getLastSql());
        $this->assign('user',$ul);

        $data=$article->find($id);
        $this->assign('blog',$data);

        $this->display();
    }
//产品信息
    public function save2()
    {
        $article = M('chanpin');           //1.参数，表名
        $data = $article->create();
        if (!$data['cp_changming'] == "") {
            if (!$data['cp_cd'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Shop/Index/Adxinxi'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }
//添加地址
    public function dizhi(){

        $article = M('yhdizhi');           //1.参数，表名
        $data = $article->create();
        var_dump($data);

        if (!$data['yh_sheng'] == "") {
            if (!$data['yh_shi'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Shop/Index/site'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }
    }


//管理地址页
    public function site(){
        $article = M("yhdizhi");                //数据表名
        $p = I("get.page");

        $totolCount = $article->count();
        $pager = new Page($totolCount, 6);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');

        $data = $article->page($p, 6)->select();
        $this->assign('lists', $data);
        $this->assign('pager', $pager);

        $u = M('username');
//        var_dump(session('username_id'));
        if(!session('username_id')==null) {
            $user = $u->find(session('username_id'));
//            var_dump($user);
        }
        $this->assign('user',$user);

        $this->display();
    }

//待支付
    public function daizhifu(){
        $article = M("yhdizhi");                //数据表名
        $p = I("get.page");

        $totolCount = $article->count();
        $pager = new Page($totolCount, 6);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');

        $data = $article->page($p, 6)->select();
        $this->assign('lists', $data);
        $this->assign('pager', $pager);



        $brticle = M('spzhifu');
        $fid=$brticle->max('id');
        var_dump( $brticle->max('id'));


        $f=$brticle->find($fid);
         var_dump($f);
         var_dump($f['sp_id']);
        $this->assign('foot', $f);


        $crticle=D('shangping');
        $k=$crticle->find($f['sp_id']);
//        var_dump($k);
        $this->assign('blog', $k);



        $ccc=$f['sp_num']*$k{'sp_xianjian'};
        $this->assign('zong',$ccc);



        $u = M('username');
        $ul = $u->create();
        $ul['username_id']=session('username_id');
//      var_dump($ul);
        if ($ul['username_id']){
            $ul = $u->find($ul['username_id']);
        } else {
            $ul = null;
        }
//      var_dump($u->getLastSql());
        $this->assign('user',$ul);

        $this->display();
    }


//删除地址
    public function deldizhi(){
        $article = D('yhdizhi');
        $id=$_GET['id'];
        $f=$article->find($id);

        if ($f) {
            $r = $article->delete();
            if ($r) {
                $this->success("数据删除成功",U('/Shop/Index/site'),3);
            } else {
                $this->error("删除失败");
            }
        } else{
            echo "sorry";
        }
    }


//修改地址页面
    public function xiugaidizhi(){
        $article = M("yhdizhi");
        $id = $_GET['id'];
        $data = $article->find($id);
        $this->assign('blog', $data);

        $this->display();
    }

//修改地址
    public function xgdizhi(){
        $article = M('yhdizhi');
        $f = $article->create();
        var_dump($f);
        if(!$f['yh_sheng']==""){
            if(!$f['yh_shi']==""){
                if ($f){
                    $r = $article->save();
                    var_dump($r);
                    if ($r) {
                        $this->success("数据修改成功",U('/Shop/Index/daizhifu'),3);
                    } else {
                        $this->error("发布失败");
                    }
                } else{
                    echo "sorry";
                }
            }else{
                $this->error('内容不能为空');
            }
        }else{
            $this->error('标题不能为空');
        }
    }

//支付表
    public function zhifu(){
        $article = M('spzhifu');           //1.参数，表名
        $data = $article->create();
        var_dump($data);

        if (!$data['sp_tc'] == "") {
            if (!$data['sp_num'] == "") {
                if ($data) {
                    $r = $article->add($data);        //3.在insert方法中使用$article->add()可以执行新增数据的操作
                    if ($r) {
                        $this->success("数据添加成功", U('/Shop/Index/daizhifu'), 3);
                    } else {
                        $this->error("发布失败");
                    }
                } else {
                    echo "sorry";
                }
            } else {
                $this->error('内容不能为空');
            }
        } else {
            $this->error('标题不能为空');
        }



    }

//用户订单页面
    public function dingdan(){
        $u = M('username');
//        var_dump(session('username_id'));
        if(!session('username_id')==null) {
            $user = $u->find(session('username_id'));
//            var_dump($user);
        }
        $this->assign('user',$user);


        $crticle = M('spzhifu');
        $p = I("get.page");

        $totolCount = $crticle->count();
//        var_dump($totolCount);
        $pager = new Page($totolCount, 10);

        $pager->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');        //共多少条记录
        $pager->setConfig('header', '<span class=“rows” style="color: dodgerblue">共 <i style="color: red">%TOTAL_ROW%</i> 条记录</span>');

        $data = $crticle->page($p, 10)->select();
        var_dump($data);
        $this->assign('lists', $data);

        $this->assign('pager', $pager);



//        $kk=D('shangping');                              //暂时沉封
//        $k=$kk->find($data[0]['sp_id']);
//        $k=$crticle->find($f['sp_id']);
//        var_dump($k);
//        $this->assign('blog', $k);

        $this->display();
    }
//管理员上传页面
    public function Adsc(){
        $v = M('guangliyuan');
//        var_dump(session('Adusername_id'));
        if (!session('Adusername_id')==null){
            $vip = $v->find(session('Adusername_id'));
//            var_dump($vip);
        }
        $this->assign('Aduser',$vip);
        $this->display();
    }
//管理员消息页面
    public function Adxiaoxi(){
        $v = M('guangliyuan');
//        var_dump(session('Adusername_id'));
        if (!session('Adusername_id')==null){
            $vip = $v->find(session('Adusername_id'));
//            var_dump($vip);
        }
        $this->assign('Aduser',$vip);

                    //时间
        $nowTime = time();
        $this->assign('nowTime', $nowTime);

        $this->display();
    }
//管理员个人资料
    public function Adper(){
        $v = M('guangliyuan');
//        var_dump(session('Adusername_id'));
        if (!session('Adusername_id')==null){
            $vip = $v->find(session('Adusername_id'));
//            var_dump($vip);
        }
        $this->assign('Aduser',$vip);
        $this->display();
    }
//产品信息
    public function Adxinxi(){
        $v = M('guangliyuan');
//        var_dump(session('Adusername_id'));
        if (!session('Adusername_id')==null){
            $vip = $v->find(session('Adusername_id'));
//            var_dump($vip);
        }
        $this->assign('Aduser',$vip);
        $this->display();
    }
//用户购物车
    public function car(){
        $u = M('username');
//        var_dump(session('username_id'));
        if(!session('username_id')==null) {
            $user = $u->find(session('username_id'));
//            var_dump($user);
        }
        $this->assign('user',$user);



        $this->display();
    }
}

